// pages/article/ArticleSearch.js
let network = null;
let utils = null;
let api = null;
const app = getApp();
var Zan = require('../../dist/index');
let start = 0;

Page(Object.assign({}, Zan.TopTips, {

  /**
   * 页面的初始数据
   */
  data: {
    inputValue: '',
    article: [],
    isshow: false,
    nodata: {
      msg: '该文章不存在',
      notype: 2,
      imgurl: '/images/nodatas.png',
      nourl: ''
    }
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    network = new app.NetWorkUtils();
    utils = new app.Utils();
    api = new app.Api();
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },
  /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    if (this.data.article.length < this.data.recordsTotal) {
      start = start + 20;
      network._post_page_json(this, api.getAriticleList, start, { title: this.data.typeId }, success);
    }
  },

  /**
   * 用户点击右上角分享
   */
  // onShareAppMessage: function () {

  // },

  bindSearchInput: function (e) {
    this.setData({
      inputValue: e.detail.value
    })
  },

  search: function (e) {
    start = 0;
    wx.stopPullDownRefresh();
    this.setData({
      isshow: true,
      article: [],
    });
    network._post_page_json(this, api.getAriticleList, start, { title: e.detail.value }, success);
  },

  /**
   * 取消
   */
  cancel: function () {
    wx.navigateBack({
      delta: 1
    })
  },

  /**
    * 点击商品进入商品详情
    */
  onArticleItemClick: function (e) {
    var item = e.currentTarget.dataset.item;
    wx.navigateTo({
      url: '/pages/article/ArticleDetails?articleId=' + item.id
    })
  }

}))

// 获取商品搜索结果
var success = function (that, res) {
  if (res.data.data) {
    for (let j = 0; j < res.data.data.length; j++) {
      res.data.data[j].createDate = utils.formatDate06(new Date(res.data.data[j].createDate))
    }
    that.setData({
      article: that.data.article.concat(res.data.data),
      recordsTotal: res.data.recordsTotal
    })
  }
}